package com.salesmanager.web.admin.controller.book;

import com.salesmanager.core.business.book.Book;
import com.salesmanager.core.business.book.service.BookService;
import com.salesmanager.core.business.generic.exception.ServiceException;
import com.salesmanager.core.business.statistics.model.BookCriteria;
import com.salesmanager.web.admin.controller.AbstractController;
import com.salesmanager.web.admin.controller.ControllerConstants;
import com.salesmanager.web.admin.entity.catalog.BookConfiguration;
import com.salesmanager.web.entity.book.BookAttribute;
import com.salesmanager.web.model.paging.PaginationDataList;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@RequestMapping("/book")
@Controller
public class BookController extends AbstractController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/bookList")
    public String productList(@ModelAttribute("bookAttribute") BookAttribute bookAttribute, Model model, HttpServletRequest request, HttpServletResponse response) {
        super.setMenu(model, request, Arrays.asList("book-manage", "book-manage"), "book-manage");
        BookCriteria criteria = new BookCriteria();
        //分页
        criteria.setMaxCount(bookAttribute.getPageSize());
        criteria.setStartIndex(bookAttribute.getOffset() - 1);
        //编码
        if (!StringUtils.isBlank(bookAttribute.getBookCode())) {
            criteria.setBookCode(bookAttribute.getBookCode());
        }
        //名称
        if (!StringUtils.isBlank(bookAttribute.getBookName())) {
            criteria.setBookName(bookAttribute.getBookName());
        }

        //id
        if (bookAttribute.getBookId() != null) {
            criteria.setBookId(bookAttribute.getBookId());
        }
        PaginationDataList<Book> dataList = bookService.getBookList(criteria);
        model.addAttribute("dataList", dataList);
        return ControllerConstants.Tiles.Book.bookList;
    }


    @RequestMapping("/addBookPage")
    public String addProductPage(Model model, HttpServletRequest request, HttpServletResponse response) {
        super.setMenu(model, request, Arrays.asList("book-manage", "book-manage"), "book-manage");
        String bookId = request.getParameter("bookId");
        BookConfiguration configuration = new BookConfiguration();
        Book book = null;
        if (!StringUtils.isBlank(bookId)) {
            book = bookService.getById(Long.parseLong(bookId));
            configuration.setBook(book);
        } else {
            book = new Book();
            configuration.setBook(book);
        }
        model.addAttribute("configuration", configuration);
        model.addAttribute("bookId", bookId);
        return ControllerConstants.Tiles.Book.bookEdit;
    }

    @RequestMapping("/saveBook")
    public String saveProduct(@ModelAttribute("configuration") BookConfiguration configuration, Model model, HttpServletRequest request, HttpServletResponse response) throws IOException, ServiceException {
        super.setMenu(model, request, Arrays.asList("book-manage", "book-manage"), "book-manage");
        Book book  = configuration.getBook();
        if (book.getId() != null) {
            book = bookService.getById(book.getId());
            book.setBookCode(configuration.getBook().getBookCode());
            book.setCount(configuration.getBook().getCount());
            book.setBookName(configuration.getBook().getBookName());
            book.setEndTime(configuration.getBook().getEndTime());
            book.setStartTime(configuration.getBook().getStartTime());
            book.setBookDesc(configuration.getBook().getBookDesc());
        }
        if (configuration.getFirstImage() != null && !configuration.getFirstImage().isEmpty()) {
            MultipartFile file = configuration.getFirstImage();
            String fileName = file.getOriginalFilename();
            book.setImagInputStream(configuration.getFirstImage().getInputStream());
            book.setImagName(fileName);
        }
        bookService.saveOrUpdate(book);
        return "redirect:/book/bookList";
    }

    @RequestMapping("/delete")
    public String delete(@ModelAttribute("configuration") BookConfiguration configuration,String bookId, Model model, HttpServletRequest request, HttpServletResponse response) throws IOException, ServiceException {
        super.setMenu(model, request, Arrays.asList("book-manage", "book-manage"), "book-manage");
        bookService.deleteBook(bookId);
        return "redirect:/book/bookList";
    }

}
